<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Client;

class ClientController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $name = $request->input("name");
        $gender = $request->input("gender");
        $birth_date = $request->input("birth_date");
        $come_from = $request->input("come_from");
        $c = new Client();
        $c->name = $name;
        $c->gender = $gender;
        $c->birth_date = $birth_date;
        $c->come_from = $come_from;
        if(!$c->save()){
            return ['result'=>'ERROR','msg'=>"保存记录失败!",'client'=>null];
        }
        return ['result'=>'OK','msg'=>'','client'=>$c];
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $c = Client::find($id+0);
        if(!$c){
            return ['result'=>'ERROR','msg'=>'获取 id 为: '.$id.' 的记录失败!','client'=>null];
        }
        return ['result'=>'OK','msg'=>'','client'=>$c];
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $c = Client::find($id+0);
        if(!$c){
            return ['result'=>'ERROR','msg'=>'要修改的 id 为: '.$id.' 的记录不存在!'];
        }
        $c->name = $request->input('name');
        $c->gender = $request->input('gender');
        $c->birth_date = $request->input('birth_date');
        $c->come_from = $request->input('come_from');
        if(!$c->save()){
            return ['result'=>'ERROR','msg'=>'保存记录失败!','client'=>null];
        }
        return ['result'=>'OK','msg'=>'','client'=>$c];
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $row_count = Client::destroy($id+0);
        if($row_count==0){
            return ['result'=>'ERROR','msg'=>'删除记录失败!'];
        }
        return ['result'=>'OK','msg'=>''];
    }

    public function getClients(){
        return Client::all();
    }
}
